<template>
	<view>
		<view class="goods-box">
			<view class="head">采购商品</view>
			<view class="" v-if="info.products.length>0">
				<view class="product-list" v-for="(item,index) in info.products" :key="index">
					<view class="name flex flai fljcsb">
						<text class="line-clamp-one flex1">s{{item.goodsName}}</text>
						<image src="../../static/img/delIcon.png" mode="" class="del" @click="delPackProduct(index,item.id)"></image>
					</view>
					<view class="store-name flex flai">
						<image src="../../static/img/store001.png" mode=""></image>
						<text>{{item.merchantName}}</text>
					</view>
					<view class="flex flai fljcsb">
						<view class="store-address flex flai flex1">
							<image src="../../static/img/ding02.png" mode=""></image>
							<text>{{item.storeLocation}}</text>
						</view>
						<view class="store-buy-num flex flai">
							<image src="../../static/img/sumIcon.png" mode="" @click="sum(index,item.id)"></image>
							<input type="text" v-model="item.number" class="flex1" disabled>
							<image src="../../static/img/addIcon.png" mode="" @click="add(index,item.id)"></image>
						</view>
					</view>
				</view>
			</view>
			<view class="no-data" v-else>
				您还没有商品哟，快去采购吧
			</view>
			<view class="purcharse" @click="toPurcharse">去采购</view>
		</view>
		<view class="info-box">
			<view class="head">发货人信息</view>
			<view class="form">

				<view class="form-group flex flai">
					<text>姓名</text>
					<input type="text" class="flex1" placeholder="请输入姓名" v-model="info.fjrName">
				</view>
				<view class="form-group flex flai">
					<text>电话</text>
					<input type="number" class="flex1" placeholder="请输入电话" v-model="info.fjrPhone">
				</view>
				<view class="form-group flex flai">
					<text>地址</text>
					<input type="text" class="flex1" placeholder="请输入地址" v-model="info.fjrAddress">
				</view>
				<view class="form-group flex flai">
					<text>备注</text>
					<input type="text" class="flex1" placeholder="请输入备注" v-model="info.fjrRemark">
				</view>
			</view>
		</view>
		<view class="info-box">
			<view class="head">收货人信息</view>
			<view class="form">
				<view class="form-group flex flai">
					<text>姓名</text>
					<input type="text" class="flex1" placeholder="请输入姓名" v-model="info.sjrName">
				</view>
				<view class="form-group flex flai">
					<text>电话</text>
					<input type="number" class="flex1" placeholder="请输入电话" v-model="info.sjrPhone">
				</view>
				<view class="form-group flex flai">
					<text>地址</text>
					<input type="text" class="flex1" placeholder="请输入地址" v-model="info.sjrAddress">
				</view>
				<view class="form-group flex flai">
					<text>备注</text>
					<input type="text" class="flex1" placeholder="请输入备注" v-model="info.sjrRemark">
				</view>
			</view>
		</view>
		<view style="height: 170rpx;"></view>
		<cover-view class="footer-box">
			<cover-view class="save" @click="savePack">保存</cover-view>
		</cover-view>
	</view>
</template>

<script>
	import API from '../../api/index.js'
	export default {
		data() {
			return {
				num: 10,
				type: 'add',
				id: null,
				info: {
					packId:"",
					products: [],
					sjrAddress: "", // 收件人地址
					fjrName: "", // 发货人姓名
					fjrRemark: "", //发货备注
					fjrAddress: "", //发货人地址
					sjrRemark: "", //收件人、备注
					fjrPhone: "", // 发货人手机号
					sjrName: "", // 收件人姓名
					sjrPhone: "", //收件人电话
				}
			}
		},
		onLoad(opt) {
			this.type = opt.type
			if (opt.type == 'add') {
				uni.setNavigationBarTitle({
					title: '新增包裹'
				});
			} else {
				uni.setNavigationBarTitle({
					title: '编辑包裹'
				});
				
				
			}
		},
		onShow () {
			if (uni.getStorageSync('packId')) {
				this.type='edit'
				this.getDetail()
			}
			
		},
		methods: {
			getDetail() {
				var id = uni.getStorageSync('packId')
				API.indexHome.getPackDetail({packId:id}).then(res=>{
					if (res.errno == '0') {
						this.info = res.data
					
					this.info.id = uni.getStorageSync('packId')
					console.log(this.info)
					}
				})
			},
			// 去采购
			toPurcharse() {
				uni.navigateTo({
					url: '/pages/search/search?type='+this.type +'&packId=' + this.info.packId
				})
			},
			// 加
			add(index, id) {
				var list = this.info.products
				console.log(list[index])
				list[index].number = list[index].number + 1
				this.info.products = list
				var num = this.info.products[index].number
				this.carNumPost(id, num)
			},
			// 减
			sum(index, id) {
				var list = this.info.products
				if (list[index].number == 1) {
					return
				} else {
					list[index].number = list[index].number - 1
					this.info.products = list
					var num = this.info.products[index].number
					this.carNumPost(id, num)
				}

			},
			carNumPost(id, num) {
				API.indexHome.addAndSumCarNum({
					itemid: id,
					number: num
				}).then(res => {
					if (res.ennro == '0') {

					}
				})
			},
			showToast(msg) {
				uni.showToast({
					title: msg,
					duration: 2000,
					icon: 'none'
				});
				return false
			},
			validate() {
				var phoneRegex = /^1\d{10}$/
				if (this.info.products.length == 0) {
					this.showToast('请采购商品')

				} else if (this.info.fjrName == '') {
					this.showToast('发货人姓名不能为空')
					
				} else if (!phoneRegex.test(this.info.fjrPhone)) {

					this.showToast('请输入正确的发货人手机号')
					
				} else if (this.info.fjrAddress == '') {
					this.showToast('请输入发货人地址')
				} else if (this.info.sjrName == '') {
					this.showToast('收件人姓名不能为空')
				} else if (!phoneRegex.test(this.info.sjrPhone)) {
					this.showToast('请输入正确的收件人手机号')
				} else if (this.info.sjrAddress == '') {
					this.showToast('请输入收件人地址')
				} else {
					return true
				}
				
			},
			// 删除包裹里面的产品
			delPackProduct(index,id) {
				API.indexHome.delPackProduct({id:id}).then(res => {
					if (res.errno == '0') {
						uni.showToast({
							title: '删除成功',
							duration: 2000
						});
						var list = JSON.parse(JSON.stringify(this.info.products))
						list.splice(index,1)
						this.info.products = list
						
					}
				})
			},
			savePack() {
				var info = JSON.parse(JSON.stringify(this.info))
				var products = [] 
				var goods = info.products
				for (var k in goods) {
					products.push({
						"number":goods[k].number,
						"goodsId":goods[k].goodsId,
						"price":goods[k].price,
						"id":goods[k].id,
						"goodsName":goods[k].goodsName
					})
				}
				info.products=products
				if (this.type == 'add') {
					if (this.validate()) {
						API.indexHome.addPackage(info).then(res => {
							if (res.errno == '0') {
								uni.showToast({
									title: '保存成功',
									duration: 2000
								});
								setTimeout(()=>{
									wx.switchTab({
										url:'/pages/writeOrder/writeOrder'
									})
								},2000)
							}
						})
					}
				} else {
					if (this.validate()) {
						
						API.indexHome.editPackage(info).then(res => {
							if (res.errno == '0') {
								uni.showToast({
									title: '保存成功',
									duration: 2000
								});
								setTimeout(()=>{
									wx.switchTab({
										url:'/pages/writeOrder/writeOrder'
									})
								},2000)
							}
						})
					}
				}

			}
		}
	}
</script>

<style>
	@import url("./index.css");
</style>
